package com.im.immodel.config

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.hotpot.sdk.room.entity.receive.CurrentSessionReceive
import com.hotpot.sdk.utils.Constants
import com.hotpot.sdk.utils.GROUP
import com.hotpot.sdk.utils.JsonUtils
import com.hotpot.uikit.databinding.HpuiItemTalkingDocBinding
import com.hotpot.uikit.view.imlist.IMAdapter
import com.hotpot.uikit.view.imlist.IMMessageConfig
import com.hotpot.uikit.view.imlist.IMMessageType
import com.hotpot.uikit.view.imlist.IMViewHolder
import com.im.immodel.R
import com.qgb.document_common.model.LinkEntity
import com.qgb.zutils.file.FileUtils
import com.qgb.document_common.utils.getFileTypeName
import com.zjkj.qgbui.util.setClick
import com.zjkj.qgbui.util.setFastClick

/**
 * @ClassName DocViewHolder
 * @Description TODO 文件自定消息
 * @Author ryan-tang
 * @Date 2021/7/13 12:01
 * @Version 1.0
 */
class DocViewHolder1 : IMViewHolder() {
    override fun msgTypeCode(): Int {
        return Constants.LINK_CODE
    }

    override fun compareYourself(holder: RecyclerView.ViewHolder): Boolean {
        return holder is ViewHolderDoc
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ViewHolderDoc(
                DataBindingUtil.inflate(
                        LayoutInflater.from(parent.context),
                        R.layout.hpui_item_talking_doc,
                        parent,
                        false
                )
        )
    }

    override fun onBindViewHolder(
            holders: RecyclerView.ViewHolder,
            position: Int,
            entity: Any,
            adapter: IMAdapter?,
    ) {
        val holder = holders as ViewHolderDoc
        val data = entity as CurrentSessionReceive
        val imConfig = adapter?.imConfig
        data.isChoice = imConfig?.isExistMultiSwitch(data.sequenceId) == true
        try {
            holder.getBinding().account = imConfig?.account
            holder.getBinding().entity = data
            holder.getBinding().isSelf = imConfig?.isSelf
            holder.getBinding().showName = TextUtils.equals(data.type, GROUP)
            val linkEntity = JsonUtils.toBean(data.text, LinkEntity::class.java)
            //1.5.5版本聊天界面下载功能暂时去掉
            holder.getBinding().showDownload = false
            holder.getBinding().isDelete = linkEntity.authId == 4
            val fileTypeName = linkEntity.fileName?.getFileTypeName()
            holder.getBinding().onlySave = false
            holder.getBinding().canEdit = FileUtils.docTypeEdit.contains(fileTypeName)
            holder.getBinding().imageVideo =
                    FileUtils.docTypeImg.contains(fileTypeName) || FileUtils.docTypeVideo.contains(fileTypeName) || !FileUtils.docTypePreview.contains(fileTypeName)
            holder.getBinding().linkEntity = linkEntity
            holder.getBinding().searchResult = imConfig?.historySequenceId == data.sequenceId
//            holder.getBinding().showChoice =
//                imConfig?.multiSwitch == true && linkEntity.tip.startsWith("创建人")
            holder.getBinding().showChoice =
                    imConfig?.multiSwitch

            holder.getBinding().isMark = imConfig?.isMarkInfo
            holder.getBinding().executePendingBindings()
        } catch (e: Exception) {
            LogUtils.e(e)
        }

        holder.getBinding().resend.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.resend)
        }
        holder.getBinding().userIcon.setOnClickListener { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.iconUser)
        }
        holder.getBinding().userIcon.setOnLongClickListener { v ->
            onItemLongClick(imConfig!!, v, position, entity, IMMessageType.longIconUser)
            true
        }
        holder.getBinding().root.setClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.confirm)
        }

        holder.getBinding().tvPreview.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.content)
        }

        holder.getBinding().tvEditOnline.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.mEditOnline)
        }

        holder.getBinding().llSave.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.mSave)
        }
        holder.getBinding().ivCancel.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.mCancel)
        }

        holder.getBinding().frameLayout.setOnLongClickListener { v -> //复制，撤回,删除
            onItemLongClick(imConfig!!, v, position, entity, IMMessageType.longPopup)
            true
        }
        holder.getBinding().frameLayout.setClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.content)
        }
        holder.getBinding().readNum.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.readNum)
        }
        holder.getBinding().tvDownload.setFastClick { v ->
            onItemClick(imConfig!!, v, position, entity, IMMessageType.mDownLoad)
        }
        holder.getBinding().choiceIv.setClick {
            multiChoice(entity, imConfig, it, position)
        }
        holder.getBinding().choiceIv2.setClick {
            multiChoice(entity, imConfig, it, position)
        }
    }

    private fun onItemLongClick(
            imConfig: IMMessageConfig,
            v: View,
            position: Int,
            entity: CurrentSessionReceive,
            type: IMMessageType,
    ) {
        if (!imConfig.multiSwitch) {
            imConfig.listener?.onLongItemClickListener(v, position, entity, type)
        } else {
            return
        }
    }

    private fun onItemClick(
            imConfig: IMMessageConfig,
            v: View,
            position: Int,
            entity: CurrentSessionReceive,
            type: IMMessageType,
    ) {
        if (!imConfig.multiSwitch) {
            imConfig.listener?.onItemClickListener(v, position, entity, type)
        } else {
            multiChoice(entity, imConfig, v, position)
        }
    }

    private fun multiChoice(
            entity: CurrentSessionReceive,
            imConfig: IMMessageConfig?,
            it: View,
            position: Int,
    ) {
        entity.isChoice = !entity.isChoice
        imConfig?.listener?.onItemClickListener(
                it,
                position,
                entity,
                IMMessageType.multiChoice
        )
    }

    internal class ViewHolderDoc(private val binding: HpuiItemTalkingDocBinding) :
            RecyclerView.ViewHolder(binding.root) {
        fun getBinding(): HpuiItemTalkingDocBinding {
            return binding
        }

    }
}